package org.jboss.as.quickstarts.poh5helloworld;

import java.io.InputStream;
import java.net.URI;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import java.util.concurrent.atomic.AtomicInteger;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import static javax.ws.rs.core.MediaType.*;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import org.apache.log4j.Logger;

@Path("mtds")
public class MethodTest {

    private static Logger log = Logger.getLogger("lern.methodtest");
    private static Map<Integer, Model> modelDB = new ConcurrentHashMap<Integer, Model>();
    private static AtomicInteger counter = new AtomicInteger();
    private @Context
    UriInfo uriinfo;

    @XmlRootElement
    public static class Model {
        @XmlAttribute
        public int id;
        public String field1;
        public String field3;

        @Override
        public String toString() {
            return "[model[id=" + id + "; field1=" + field1 + "; field3=" + field3 + "]]";
        }
    }

    @GET
    @Path("fill/{count}")
    public Response doFill(@PathParam("count") int count) {
        for (int i = 0; i < count; i++) {
            Model buf = new Model();
            buf.id = counter.incrementAndGet();
            buf.field1 = "filed1-" + buf.id;
            buf.field3 = "filed3-" + buf.id;
            modelDB.put(buf.id, buf);
        }
        //BaseUri:http://localhost:8080/hello/
        //getPath: mtds/fill/7
        log.info("uriinfo.getBaseUri():" + uriinfo.getBaseUri() + "; uriinfo.getPath():" + uriinfo.getPath());
        return Response.created(URI.create(uriinfo.getBaseUri() + "mtds/list")).build();
    }

    @GET
    @Path("list")
    @Produces({TEXT_PLAIN,APPLICATION_XML})
    public String doList() {
        /*
        StringBuilder buf = new StringBuilder(12);
        for (Model m : modelDB.values()) {
            buf.append(m.toString());
            buf.append("\n");
        }
        return buf.toString();
        */
        return modelDB.toString();
    }

    @POST
    @Consumes(APPLICATION_JSON)
    public Response doPost(Model model) {


        model.id = counter.incrementAndGet();
        modelDB.put(model.id, model);
        return Response.created(URI.create("mtds/" + model.id)).build();
    }

    @GET
    @Path("{id}")
    @Produces({APPLICATION_XML, APPLICATION_JSON})
    public Model getModel(@PathParam("id") int id) {
        Model result = modelDB.get(id);
        if (result != null) {
            return result;
        } else {
            throw new WebApplicationException(Response.Status.NOT_FOUND);
        }
    }

    /*
     @Path("{id}")
     @Produces(APPLICATION_XML)
     public Model doPost(@PathParam("id") int id) {
     Model result = modelDB.get(id);
     if (result != null) {
     return result;
     } else {
     throw new WebApplicationException(Response.Status.NOT_FOUND);
     }
     }*/
}
